package com.sxm.yiwei.pinche;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sxm.yiwei.R;
import com.sxm.yiwei.adapters.pinche.routeAdapter;
import com.sxm.yiwei.base.BaseActivity;
import com.sxm.yiwei.beans.pinche.HostOrder;
import com.sxm.yiwei.beans.pinche.Route;
import com.sxm.yiwei.constant.Common;
import com.sxm.yiwei.constant.Constant;
import com.sxm.yiwei.tools.BookPayActivity;
import com.sxm.yiwei.views.MyListview;

import java.util.ArrayList;

public class PriceDifferenceActivity extends BaseActivity {

    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView tv_seats_pre;
    private ImageView iv_icon_pre;
    private TextView tv_phone_pre;
    private TextView tv_host_words_pre;
    private TextView tv_start_pos_pre;
    private TextView tv_end_pos_pre;
    private TextView tv_order_code_pre;
    private TextView tv_depart_time_pre;
    private TextView tv_total_price;
    private CheckBox checkbox;
    private TextView tv_refund_change_rule;
    private TextView tv_buchajia;
    private TextView tv_pay_now;
    private ArrayList<Route> selectedRoutes;
    private MyListview myListView;
    private routeAdapter routeAdapter;
    private double grapAmount;
    private String mixOrderId;
    private LinearLayout ll_two;
    private LinearLayout ll_complete;
    private TextView tv_complete_change;

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_price_difference);

    }

    @Override
    public void initView() {
        rl_back = (RelativeLayout)findViewById(R.id.rl_back);
        tv_title = (TextView)findViewById(R.id.tv_title);
        tv_seats_pre = (TextView)findViewById(R.id.tv_seats_pre);
        iv_icon_pre = (ImageView)findViewById(R.id.iv_icon_pre);
        tv_phone_pre = (TextView)findViewById(R.id.tv_phone_pre);
        tv_host_words_pre = (TextView)findViewById(R.id.tv_host_words_pre);
        tv_start_pos_pre = (TextView)findViewById(R.id.tv_start_pos_pre);
        tv_end_pos_pre = (TextView)findViewById(R.id.tv_end_pos_pre);
        tv_order_code_pre = (TextView)findViewById(R.id.tv_order_code_pre);
        tv_depart_time_pre = (TextView)findViewById(R.id.tv_depart_time_pre);
        tv_total_price = (TextView)findViewById(R.id.tv_total_price);
        checkbox = (CheckBox)findViewById(R.id.checkbox);
        tv_refund_change_rule = (TextView)findViewById(R.id.tv_refund_change_rule);
        tv_buchajia = (TextView)findViewById(R.id.tv_buchajia);
        tv_pay_now = (TextView)findViewById(R.id.tv_pay_now);
        myListView = (MyListview)findViewById(R.id.myListView);
        ll_two = (LinearLayout)findViewById(R.id.ll_two);
        ll_complete = (LinearLayout)findViewById(R.id.ll_complete);
        tv_complete_change = (TextView)findViewById(R.id.tv_complete_change);

        tv_pay_now.setBackgroundColor(getResources().getColor(R.color.price_differ_enable));
        tv_pay_now.setEnabled(false);
        tv_complete_change.setBackgroundColor(getResources().getColor(R.color.price_differ_enable));
        tv_complete_change.setEnabled(false);
    }

    @Override
    public void initListener() {
        rl_back.setOnClickListener(this);
        tv_refund_change_rule.setOnClickListener(this);
        tv_buchajia.setOnClickListener(this);
        tv_pay_now.setOnClickListener(this);
        tv_complete_change.setOnClickListener(this);
        checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (!b){
                    tv_pay_now.setBackgroundColor(getResources().getColor(R.color.price_differ_enable));
                    tv_pay_now.setEnabled(false);
                    tv_complete_change.setBackgroundColor(getResources().getColor(R.color.price_differ_enable));
                    tv_complete_change.setEnabled(false);
                }else {
                    tv_pay_now.setBackgroundColor(getResources().getColor(R.color.textcolorh));
                    tv_pay_now.setEnabled(true);
                    tv_complete_change.setBackgroundColor(getResources().getColor(R.color.textcolorh));
                    tv_complete_change.setEnabled(true);
                }
            }
        });
    }

    @Override
    public void initData() {
        selectedRoutes = getIntent().getParcelableArrayListExtra("selected_routes");
        grapAmount = getIntent().getDoubleExtra("grapAmount",-1);
        mixOrderId = getIntent().getStringExtra("mixOrderId");
        tv_title.setText("补差价");
        if (selectedRoutes!=null){
            routeAdapter = new routeAdapter(this,selectedRoutes);
            myListView.setAdapter(routeAdapter);
        }
        if (grapAmount==0){
            ll_complete.setVisibility(View.VISIBLE);
            ll_two.setVisibility(View.GONE);
        }else {
            ll_two.setVisibility(View.VISIBLE);
            ll_complete.setVisibility(View.GONE);
            if (grapAmount>0){
                tv_buchajia.setText("需补差价"+grapAmount+"元");
                tv_pay_now.setText("立即支付");
            }else {
                tv_buchajia.setText("退还金额"+(-grapAmount)+"元");
                tv_pay_now.setText("完成改签");
            }
        }
    }

    private void formOrder() {
        JSONObject object = new JSONObject();
        JSONArray array = new JSONArray();
        for (int i = 0; i < selectedRoutes.size(); i++) {
            Route route = selectedRoutes.get(i);
            JSONObject obj = new JSONObject();
            obj.put("ownerId",route.getOwnerId());
            obj.put("serviceId",route.getServiceId());
            obj.put("pinTotalAccount",route.getPinTotalAccount());
            obj.put("passerRequireSeats",route.getPasserRequireSeats());
            array.add(obj);

        }
        object.put("serviceList",array);

        httpPost(Common.HOST+Common.CHANGE_FORM_ORDER,object, Constant.CHANGE_FORM_ORDER);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.tv_refund_change_rule:
                Intent iRCRule = new Intent(this, RuleActivity.class);
                iRCRule.putExtra("type",Constant.REFUND_CHANGE_RULE);
                startActivity(iRCRule);
                break;
            case R.id.tv_buchajia:
                if (grapAmount>0){

                }else if (grapAmount<0){

                }
                break;
            case R.id.tv_pay_now:
                if (grapAmount>0){
                    Intent intent = new Intent(this, BookPayActivity.class);
                    intent.putExtra("orderCode",mixOrderId);
                    intent.putExtra("amount", grapAmount+"");
                    intent.putExtra("from", Constant.CHANGE_PAY);
                    intent.putExtra("id","");
                    intent.putExtra("serviceList",selectedRoutes);
                    intent.putExtra("alipay", Common.CHANGE_ALI_PAY);
                    intent.putExtra("yivipay",Common.CHANGE_REST_PAY);
                    intent.putExtra("type",Constant.CHANGE_OK);
                    intent.putExtra("text_1","");//需要订单生成时间
                    intent.putExtra("text_2","");//需要mixOrderCode
                    startActivity(intent);
                }else if (grapAmount<0){
                    JSONObject object = new JSONObject();
                    object.put("orderId",mixOrderId);
                    object.put("grapAmount",grapAmount+"");
                    JSONArray array = new JSONArray();
                    for (int i = 0; i < selectedRoutes.size(); i++) {
                        Route route = selectedRoutes.get(i);
                        JSONObject obj = new JSONObject();
                        obj.put("orderId",route.getOrderId());
                        obj.put("ownerId",route.getOwnerId());
                        obj.put("serviceId",route.getServiceId());
                        obj.put("pinTotalAccount",route.getTotalAmount());
                        obj.put("passerRequireSeats",route.getPasserRequireSeats());
                        //Log.d("changeFormOrder"," pinTotalAccount="+route.getTickets()*hostOrder.getSingle_money()+" passerRequireSeats="+hostOrder.getTickets()+" pinLoseAccount="+hostOrder.getLoss_money());
                        array.add(obj);
                    }
                    object.put("serviceList",array.toString());
                    httpPost(Common.HOST+Common.COMPLETE_CHANGE,object,Constant.COMPLETE_CHANGE);
                }

                break;
            case R.id.tv_complete_change:
                JSONObject object = new JSONObject();
                object.put("orderId",mixOrderId);
                object.put("grapAmount",grapAmount+"");
                JSONArray array = new JSONArray();
                for (int i = 0; i < selectedRoutes.size(); i++) {
                    Route route = selectedRoutes.get(i);
                    JSONObject obj = new JSONObject();
                    obj.put("orderId",route.getOrderId());
                    obj.put("ownerId",route.getOwnerId());
                    obj.put("serviceId",route.getServiceId());
                    obj.put("pinTotalAccount",route.getTotalAmount());
                    obj.put("passerRequireSeats",route.getPasserRequireSeats());
                    //Log.d("changeFormOrder"," pinTotalAccount="+route.getTickets()*hostOrder.getSingle_money()+" passerRequireSeats="+hostOrder.getTickets()+" pinLoseAccount="+hostOrder.getLoss_money());
                    array.add(obj);
                }
                object.put("serviceList",array.toString());
                httpPost(Common.HOST+Common.COMPLETE_CHANGE,object,Constant.COMPLETE_CHANGE);
                break;
        }
    }

    @Override
    public void onSuccess(String result, String fromWhere) {
        super.onSuccess(result, fromWhere);
        com.alibaba.fastjson.JSONObject obj = JSON.parseObject(result);
        if (fromWhere.equals(Constant.CHANGE_FORM_ORDER)){

        }else if (fromWhere.equals(Constant.COMPLETE_CHANGE)){
            Intent intent = new Intent(this, CompleteActivity.class);
            intent.putExtra("type",Constant.CHANGE_OK);
            intent.putExtra("text_1","");//需要订单生成时间
            intent.putExtra("text_2","");//需要mixOrderCode
        }
    }
}
